#!/usr/bin/env python
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np
# import matplotlib.font_manager
from matplotlib import rcParams

rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['HarmonyOS Sans SC', 'Microsoft YaHei']
rcParams['font.size'] = 16

data_header = (u'无预取(LRU)', u'Mithril', u'Mithril(仅未命中)', u'改进算法')
data_colname = (8, 16, 32, 64)
data_hr = (
    (0.432884, 0.454354, 0.446101, 0.455695),
    (0.446354, 0.46077, 0.454298, 0.460722),
    (0.455879, 0.465228, 0.46138, 0.46773),
    (0.463182, 0.469311, 0.46756, 0.472616),
)

data_tm = (
    (5.98976, 923.874, 184.542, 620.953),
    (5.98902, 990.381, 169.125, 509.732),
    (5.8948, 1001.38, 151.428, 470.64),
    (5.84229, 923.548, 141.383, 414.764),
)


def main_hr():
    _, ax = plt.figure(figsize=(8, 6)), plt.gca()  # Create a figure and an axes
    n = np.array(data_hr, dtype=float).transpose() * 100  # to 100%

    for i in range(4):
        ax.plot(range(4), n[i], linewidth=2, label=data_header[i], marker='o', markersize=6)

    ax.set_ylim([40, 48])
    ax.set_ylabel('命中率 / %')

    ax.set_xticks(range(4))
    ax.set_xticklabels(data_colname)

    ax.set_xlabel('缓存大小 / MB')
    ax.legend(data_header)

    plt.savefig('figures/hit_ratio.png')
    pass


def main_tm():
    _, ax = plt.figure(figsize=(8, 6)), plt.gca()  # Create a figure and an axes
    n = np.array(data_tm, dtype=float).mean(0) / 60  # to minutes

    b1 = ax.bar(range(4), n, linewidth=2)

    ax.bar_label(b1, fmt='%.1f')

    ax.set_ylim([0, 20])
    ax.set_ylabel('运行时间 / min')

    ax.set_xticks(range(4))
    ax.set_xticklabels(data_header)

    plt.savefig('figures/exec_time.png')
    pass


main_hr()
main_tm()
